package cn.qingyun.gis.modules.caronline.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.caronline.entity.CarAttachment;
import cn.qingyun.gis.modules.caronline.mapper.CarAttachmentMapper;
import cn.qingyun.gis.modules.caronline.service.ICarAttachmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 网约车-附件 服务实现类
 * </p>
 *
 * @author guest
 * @since 2023-10-28
 */
@Service
public class CarAttachmentServiceImpl extends ServiceImpl<CarAttachmentMapper, CarAttachment> implements ICarAttachmentService {
    @Resource
    private CarAttachmentMapper carAttachmentMapper;

    @Override
    @Transactional
    public void upload(List<String> pictureList, String id) {
        //上传图片不为空
        try {
            if (ObjectUtil.isEmpty(pictureList)) {
                log.debug("未上传图片");
            } else {
                int count = carAttachmentMapper.selectCount(new LambdaQueryWrapper<CarAttachment>().eq(CarAttachment::getCarProofreadId, id));
                if (count > 0) { //附件列表中存在上次勘误上传图片
                    carAttachmentMapper.delete(new LambdaQueryWrapper<CarAttachment>().eq(CarAttachment::getCarProofreadId, id));
                }
                List<CarAttachment> attachmentsList = Lists.newArrayList();
                for (String picture : pictureList) {
                    CarAttachment attachment = new CarAttachment();
                    attachment.setCarProofreadId(id);
                    attachment.setContent(picture);
                    attachment.setCreateTime(DateUtil.date());
                    attachmentsList.add(attachment);
                }
                this.saveBatch(attachmentsList);
            }
        } catch (Exception e) {
            throw new CommonException("图片上传失败");
        }
    }

    @Override
    public List<CarAttachment> show(String id) {
        if (CharSequenceUtil.isEmpty(id)) {
            throw new CommonException("传入id不能为空");
        }
        //返回图片信息
        return carAttachmentMapper.selectList(new LambdaQueryWrapper<CarAttachment>().eq(CarAttachment::getCarProofreadId, id));

    }
}
